package billrestauration.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

  







import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.GrantedAuthority;

import billrestauration.extra.Config;
 
@Entity
@Lazy(value=false)
@Table
public class Utilisateur  extends org.springframework.security.core.userdetails.User implements Serializable{
public final static String  PASSWORD_PRIVATE="[PRIVATE]";
	public Utilisateur(String username) {
		 super(username,"_",new ArrayList<GrantedAuthority>());
		 this.username=username;
		 
		 
	}
	public Utilisateur(String username,String password,Collection<GrantedAuthority> aut) {
		 super(username,password,aut);
		 this.username=username;
		 this.password=password;
		 
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = Config.versionUID;
	@Id
	@Column
	@GeneratedValue
	private long id;
	@Column(name="username",unique=true,length=255)
	protected String username;
	@Column(name="password",length=500)
	protected String password;
	@Column(name="enable")
	protected boolean enable;
	@Column
	private String nom;
	@Column(nullable=true)
	private String salt;
	@Column
	private String prenom;
	public String getSalt() {
		return salt;
	}
	public void setSalt(String salt) {
		this.salt = salt;
	}

	@OneToOne(targetEntity=Adresse.class)
	@JoinColumn(name="refadresse",referencedColumnName="id",nullable=true)
	private Adresse adresse;
	public Utilisateur(long id, String username, String password,
			boolean enable, String nom, String prenom, Adresse adresse) {
		super(username,password,enable,true,true,true,new ArrayList<GrantedAuthority>());
		this.id = id;
		this.username = username;
		this.password = password;
		this.enable = enable;
		this.nom = nom;
		this.prenom = prenom;
		this.adresse = adresse;
	}
	
	 

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		 
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		if(PASSWORD_PRIVATE.compareTo(password)!=0)
		this.password = password;
	}
	public boolean isEnable() {
		return enable;
	}
	public void setEnable(boolean enable) {
		this.enable = enable;
	}
	@Override
	public String toString() {
		return "Utilisateur [id=" + id + ", username=" + username
				+ ", password=" + PASSWORD_PRIVATE + ",salt="+salt+" , enable=" + enable + ", nom="
				+ nom + ", prenom=" + prenom + ", adresse=" + adresse
				+ ", Authorities=" + getAuthorities()  + "]";
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	 
	 

	 

	public String getNom() {
		return nom;
	}
	public void setNom(String nom) {
		this.nom = nom;
	}
	public String getPrenom() {
		return prenom;
	}
	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}
	
	
	public Adresse getAdresse() {
		return adresse;
	}

	public void setAdresse(Adresse adresse) {
		this.adresse = adresse;
	}

	public Utilisateur() {
		super("_","_",true,true,true,true,new ArrayList<GrantedAuthority>());
		this.adresse=new Adresse();
		this.enable=true;
		this.salt=null;
		// TODO Auto-generated constructor stub
	}
	 
	 
	
}
